﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using shfa.snomtastic.data;
using shfa.snomtastic.data.business;
using shfa.snomtastic.data.ip;


namespace shfa.snomtastic.phone
{
    public partial class phone : System.Web.UI.Page
    {

        public string host { get; set; }
        public string appPath { get; set; }
        public string macAddress { get; set; }
        public string userAgent { get; set; }
        public string ipAddress { get; set; }
        public bool phoneExists { get; set; }
        public AppConfig ac = new AppConfig();

        protected void Page_Load(object sender, EventArgs e)
        {
            macAddress = Request.QueryString["mac"];
            host = Request.Url.Authority;

            userAgent = Request.ServerVariables["HTTP_USER_AGENT"];
            ipAddress = Request.UserHostAddress;

            phoneExists = captureRequest();
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {

            Response.ContentType = "text/xml";
            Response.ContentEncoding = Encoding.UTF8;

            if (phoneExists)
            {


                using (TextWriter textWriter = new StreamWriter(Response.OutputStream, Encoding.UTF8))
                {
                    XmlWriterSettings writerSettings = new XmlWriterSettings();
                    writerSettings.OmitXmlDeclaration = false;
                    writerSettings.CloseOutput = true;

                    XmlWriter xmlWriter = XmlTextWriter.Create(textWriter, writerSettings);


                    xmlWriter.WriteStartElement("setting-files");

                    xmlWriter.WriteStartElement("file");
                    xmlWriter.WriteAttributeString("url", ac.Items["siteRoot"] + "phone/settings.aspx?mac=" + macAddress);
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteEndElement();
                    xmlWriter.Close();
                }
            }

        }


        private bool captureRequest()
        {

            PhoneController pc = new PhoneController();
            PhoneInfo pi;

            if (macAddress != null & macAddress != string.Empty)
            {
                pi = pc.SelectPhoneByMacAddress(macAddress);
                if (pi != null)
                {
                    pi.UpdatedDate = DateTime.UtcNow;
                    pi.UserAgent = userAgent;
                    pi.IpAddress = ipAddress;

                    UserAgentInfo ao = new UserAgentInfo(userAgent);

                    pi.Model = ao.Model;
                    pi.Firmware = ao.Firmware;
                    pi.BootLoader = ao.BootLoader;
                    pi.Extension = "";

                    ModelController mc = new ModelController();
                    List<ModelInfo> miList = mc.SelectModels();
                    foreach (ModelInfo mi in miList)
                    {
                        if (pi.Model == mi.Name)
                            pi.ModelId = mi.ModelId;
                    }

                    FirmwareController fc = new FirmwareController();
                    List<FirmwareInfo> fiList = fc.SelectFirmwares();
                    foreach (FirmwareInfo fi in fiList)
                    {
                        if (pi.Firmware == fi.Version & pi.ModelId == fi.ModelId)
                        {
                            pi.FirmwareId = fi.FirmwareId;
                        }
                    }


                    try
                    {
                        IPTool ip = new IPTool(ipAddress);
                        Int32 ipInt = (Int32)ip.IPAddressToNumber(ipAddress);

                        SubnetController sc = new SubnetController();
                        List<SubnetInfo> siList = sc.SelectSubnets();
                        foreach (SubnetInfo si in siList)
                        {
                            if (ipInt >= si.StartInt & ipInt <= si.EndInt)
                            {
                                pi.SubnetId = si.SubnetId;
                            }
                        }
                    }
                    catch { }

                    pc.UpdatePhone(pi);

                    return true;
                }
            }

            return false;

        }
    }


}